<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">

		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="请求地址" prop="apiPath">
				<el-input v-model="form.apiPath" clearable></el-input>
			</el-form-item>
			<el-form-item label="API分组名" prop="apiGroup">
				<el-input v-model="form.apiGroup" clearable></el-input>
			</el-form-item>
			<el-form-item label="请求方式" prop="apiMethod">
				<el-radio-group v-model="form.apiMethod">
					<el-radio-button label="GET" value="GET"></el-radio-button>
					<el-radio-button label="POST" value="POST"></el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="API描述" prop="apiInfo">
				<el-input v-model="form.apiInfo" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增APP',
				edit: '编辑APP'
			},
			form: {},
			rules: {
				apiPath: [
					{required: true, message: '请输入请求地址'}
				],
				apiMethod: [
					{required: true, message: '请选择请求方式'}
				],
				apiInfo: [
					{required: true, message: '请输入API描述信息'}
				],
				apiGroup: [
					{required: true, message: '请输入API分组名'}
				],
			},
			visible: false,
			isSaveing: false
		}
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let res = await this.$API.system.Apis.AddApis.post(this.form);
					if (res.code === 0) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						await this.$alert(res.message, "提示", {type: 'error'})
					}
					this.isSaveing = false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form = data
		}
	}
}
</script>

<style>
</style>
